import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login'
import Register from '../views/Regist'
import Write from "../views/Write";
import Search from '../views/Search'
import Info from "../views/Info";
import InfoDetail from "../views/InfoDetail";
import store from "../store";


Vue.use(VueRouter)

const routes = [
  {
    path: '/infoDetail/:name',
    name: 'InfoDetail',
    component: InfoDetail
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/info',
    name: 'Info',
    component: Info
  },
  {
    path: '/search',
    name: 'Search',
    component: Search
  },
  {
    path: '/writer',
    name: 'Writer',
    component: Write
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const that = this
router.beforeEach((to, from,next) => {

  if(to.name=='Login' || to.name == 'register'){
    console.log('是登录页面')
    console.log(to)
    next()
  }else{
    console.log('不是登录页面:登录状态=>',store.state.login)

    if(store.getters.getLogin==false){
      console.log('未登录')
      next({name:'Login'})
    }else{
      next()
    }
  }

})
export default router
